package com.yumei.trader.gym.util;

import java.io.ByteArrayOutputStream;

public  class StreamWriter {

	public static final int FloatSize = 4;
	
	public static final int IntSize = 4;
	
	
	public static int readInt(byte[] buffer) {
        return Encdec.dec_uint32le(buffer, 0);
    }


    public static int readInt(byte[] buffer, int p) {
        return Encdec.dec_uint32le(buffer, p);

    }


    public static long readLong(byte[] buffer, int p) {
        return Encdec.dec_uint64le(buffer, p);

    }

    public static int readShort(byte[] buffer, int p) {
        return Encdec.dec_uint16le(buffer, p);

    }
    
    public static float readFloat32(byte[] v) {
        return Encdec.dec_floatle(v, 0);
    }


    public static float readFloat32(byte[] v, int p) {
        return Encdec.dec_floatle(v, p);
    }
    
    public static String readString(byte[] buffer, int p, int len) {
        
    	ByteArrayOutputStream baos = new ByteArrayOutputStream();
    	
    	byte[] body = null;
    	
    	for(int i = p;i<p+len;i++){
    		baos.write(buffer[i]);
    	}
    	try{
    		body = baos.toByteArray();
    		baos.close();
    		
    		return new String(body,"UTF-8").trim();		
    	}catch(Exception e){
    		e.printStackTrace();
    	}finally{
    		baos = null;
    		body = null;
    	}
    	return null;
    }
    
    
    public static String readString(byte[] buffer, int p, int len,String encode) {
        
    	ByteArrayOutputStream baos = new ByteArrayOutputStream();
    	
    	byte[] body = null;
    	
    	for(int i = p;i<p+len;i++){
    		baos.write(buffer[i]);
    	}
    	try{
    		body = baos.toByteArray();
    		baos.close();
    		
    		return new String(body,encode).trim();		
    	}catch(Exception e){
    		e.printStackTrace();
    	}finally{
    		baos = null;
    		body = null;
    	}
    	return null;
    }
}
